const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    source: [],
  },

  onLoad: function (options) {
    let _self = this;
    //服务器端返回的角色定义数值 1维修人员  2销售 3合伙人 
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data)
      _self.setData({
        source: data
      })
    })
  },


  redirect(e) {
    const index = e.currentTarget.dataset.index;
    wx.setStorageSync('token', this.data.source[index].token)
    app.ajax({
      url: "Partner/User/InfoUpdate",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: res => {
        if (res.data.code == 200) {
          app.showToast("登录成功", "success")
          let role = this.data.source[index].role;
          wx.setStorageSync('role', role);
          //服务器端返回的角色定义数值 1维修人员  2销售 3合伙人
          if (role == 1) {
            this.getEngineerInfo();
            setTimeout(() => {
              wx.reLaunch({
                url: '../../engineering/pages/index/index',
              })
            }, 1000)
          } else if (role == 2) {
            this.getSalemanInfo();
            setTimeout(() => {
              wx.reLaunch({
                url: '../../promoter/pages/index/index',
              })
            }, 1000)
          } else {
            this.getPartnerInfo();
            setTimeout(() => {
              wx.switchTab({
                url: '../partner/index/index',
              })
            }, 1000)
          }
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },



  setLoginHistoryRoles(userInfo) {
    const loginHistoryRoles = wx.getStorageSync('loginHistoryRoles') || [];
    let role = wx.getStorageSync('role');
    userInfo.role = role //将Storage的角色打放入userInfo
    if (loginHistoryRoles.length == 0) {
      loginHistoryRoles.push(userInfo);
    } else {
      let max = 5; //最多5条历史登录数据
      let index = loginHistoryRoles.findIndex(item => {
        return item.role == role && item.id == userInfo.id
      });
      if (~index) {
        loginHistoryRoles[index] = userInfo;
      } else {
        if (loginHistoryRoles.length < max) {
          loginHistoryRoles.unshift(userInfo);
        } else {
          loginHistoryRoles.pop();
          loginHistoryRoles.unshift(userInfo);
        }
      }
    }
    wx.setStorageSync('loginHistoryRoles', loginHistoryRoles)
  },


  getPartnerInfo() {
    app.ajax({
      url: "Partner/User/getPartnerInfo",
      success: res => {
        if (res.data.code == 200) {
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.account_name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },

  //获取销售的相关信息
  getSalemanInfo() {
    app.ajax({
      url: 'Salesman/Login/getInfo',
      data: {},
      success: res => {
        if (res.data.code == 1000) {
          wx.setStorageSync('userInfo', res.data.data);
          wx.setStorageSync('salesman_id', res.data.data.id);
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.full_name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },

  //获取维修员信息
  getEngineerInfo() {
    app.ajax({
      url: 'Manage/User/getManageInfo',
      method: "POST",
      data: {},
      success: res => {
        if (res.data.code == 200) {
          wx.setStorageSync('userInfo', res.data.data);
          let origin = res.data.data;
          let userInfo = {
            id: origin.id,
            name: origin.name,
            account: origin.account,
            token: wx.getStorageSync('token')
          };
          this.setLoginHistoryRoles(userInfo);
        }
      }
    })
  },


})